//
//  YZMNavigationController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/21.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMNavigationController.h"
#import <objc/runtime.h>

@interface UINavigationController (privated)

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;

@end

@interface YZMNavigationController ()

@property (nonatomic, assign) BOOL navigationLoaded; //fit for iOS 7.0

@end


@implementation YZMNavigationController

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    BOOL autoBack = YES;
    if ([self.topViewController respondsToSelector: @selector(shouldAutoBackToPreviousController)]) {
        autoBack = [(id <YZSNavigationControllerProtocol>)self.topViewController shouldAutoBackToPreviousController];
    }
    return autoBack && [super navigationBar: navigationBar shouldPopItem: item];
}

- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item
{
    if (class_getInstanceMethod([UINavigationController class], @selector(navigationBar:didPushItem:)) != NULL) {
        [super navigationBar: navigationBar didPushItem: item];
    }
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
        if (!self.navigationLoaded && navigationBar.backItem) {
            for (UIView *view in navigationBar.subviews) {
                if ([view isKindOfClass: NSClassFromString(@"UINavigationItemButtonView")]) {
                    [view setNeedsDisplay];
                }
            }
            self.navigationLoaded = YES;
        }
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (UIViewController *)childViewControllerForStatusBarStyle
{
    return nil;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
